package com.example.myapplication.part20;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText; // 添加此行导入EditText
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Part20Activity extends AppCompatActivity implements
        DatePickerDialog.OnDateSetListener,
        TimePickerDialog.OnTimeSetListener {

    private EditText etTourName, etCustomerName, etPeopleCount, etTotalAmount;
    private TextView tvSelectedDate, tvSelectedTime;
    private RecyclerView rvOrders;
    private List<Order> orderList = new ArrayList<>();
    private int selectedYear, selectedMonth, selectedDay;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_part20);

        // 初始化视图
        initViews();

        // 设置RecyclerView
        rvOrders.setLayoutManager(new LinearLayoutManager(this));
        rvOrders.setAdapter(new OrderAdapter(orderList));

        // 日期选择
        findViewById(R.id.btnDatePicker).setOnClickListener(v -> {
            Calendar calendar = Calendar.getInstance();
            new DatePickerDialog(Part20Activity.this, Part20Activity.this,
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH)).show();
        });

        // 时间选择
        findViewById(R.id.btnTimePicker).setOnClickListener(v -> {
            Calendar calendar = Calendar.getInstance();
            new TimePickerDialog(Part20Activity.this, Part20Activity.this,
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    true).show();
        });

        // 提交订单
        findViewById(R.id.btnSubmit).setOnClickListener(v -> submitOrder());
    }

    private void initViews() {
        etTourName = findViewById(R.id.etTourName);
        etCustomerName = findViewById(R.id.etCustomerName);
        etPeopleCount = findViewById(R.id.etPeopleCount);
        etTotalAmount = findViewById(R.id.etTotalAmount);
        tvSelectedDate = findViewById(R.id.tvSelectedDate);
        tvSelectedTime = findViewById(R.id.tvSelectedTime);
        rvOrders = findViewById(R.id.rvOrders);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        selectedYear = year;
        selectedMonth = month;
        selectedDay = dayOfMonth;
        String selectedDate = year + "-" + (month + 1) + "-" + dayOfMonth;
        tvSelectedDate.setText(selectedDate);
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String selectedTime = String.format("%02d:%02d", hourOfDay, minute);
        tvSelectedTime.setText(selectedTime);
    }private void submitOrder() {
        // 数据验证
        if (!validateInput()) return;

        // 创建订单
        Order order = new Order(
                etTourName.getText().toString(),
                etCustomerName.getText().toString(),
                tvSelectedDate.getText().toString(),
                tvSelectedTime.getText().toString(),
                Integer.parseInt(etPeopleCount.getText().toString()),
                Double.parseDouble(etTotalAmount.getText().toString())
        );

        // 添加到列表
        orderList.add(order);
        rvOrders.getAdapter().notifyDataSetChanged();
        clearForm();
    }

    private boolean validateInput() {
        if (TextUtils.isEmpty(etTourName.getText())) {
            etTourName.setError("请输入旅游项目名称");
            return false;
        }
        if (TextUtils.isEmpty(tvSelectedDate.getText())) {
            Toast.makeText(this, "请选择日期", Toast.LENGTH_SHORT).show();
            return false;
        }
        // 其他字段验证...
        return true;
    }

    private void clearForm() {
        etTourName.setText("");
        etCustomerName.setText("");
        etPeopleCount.setText("");
        etTotalAmount.setText("");
        tvSelectedDate.setText("");
        tvSelectedTime.setText("");
    }
}